package edu.upenn.cis542;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;



public class PaletteView extends View{
public static Paint p;
	public PaletteView(Context context) {
		super(context);
		init();
		// TODO Auto-generated constructor stub
	}
	
	public PaletteView(Context context, AttributeSet a) {
		super(context,a);
		init();
		// TODO Auto-generated constructor stub
	}
	
	protected ShapeDrawable[] square= new ShapeDrawable[5];
	
	static int selectedColor=Color.RED;
	static int selectedThick=0; // 0->Thin 1->Thick
	protected int[] colors = new int [] {Color.RED, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW}; 
	protected void init()
	{
		for(int i=0;i<5;i++)
		{
			square[i] = new ShapeDrawable(new RectShape());
			square[i].getPaint().setColor(colors[i]);
			square[i].setBounds(i*60, 0, (i+1)*60, 0+60);
		}
	}
	
	/**
	 * If I call invalidate onDraw gets called
	 */
	protected void onDraw(Canvas canvas)
	{
		for(int i=0;i<5;i++)
		{
			square[i].draw(canvas);
		}
		
		p = new Paint();
		p.setColor(Color.WHITE);
		p.setTextAlign(Paint.Align.LEFT);
		p.setTypeface(Typeface.SANS_SERIF);
		p.setTextSize(20);
		canvas.drawText("THICK", 0, 100, p);
		canvas.drawText("thin", 100, 100, p);
		selectedColor=Color.RED;
	}
	
	public boolean onTouchEvent(MotionEvent event) 
	{
		Paint p = new Paint();
		if (event.getAction() == MotionEvent.ACTION_DOWN)
		{
			int x = (int)event.getX(); int y = (int)event.getY();
			System.out.println("X ==> " + x + " Y ==> " + y);
			
			if (x > 0 && x < 60 && y > 0 && y < 60)
			{
				selectedColor = Color.RED;
				p.setColor(Color.RED);
				
				if(PaintBrushView.colorTracker.size()!=0){
				Switcher temp=PaintBrushView.colorTracker.remove(PaintBrushView.colorTracker.size()-1); 
				temp.setEnd(PaintBrushView.xcord.size()-1);
				PaintBrushView.colorTracker.add(temp);}
				
				Switcher obj=new Switcher(PaintBrushView.xcord.size(), Color.RED, selectedThick);				
				PaintBrushView.colorTracker.add(obj);
				
				//System.out.println("*******************SELECTED RED************************");
//				for(int i=0;i<PaintBrushView.colorTracker.size();i++)
//				{
//					System.out.println("Start====>"+PaintBrushView.colorTracker.get(i).getStart());
//					System.out.println("End====>"+PaintBrushView.colorTracker.get(i).getEnd());
//					System.out.println("Color====>"+PaintBrushView.colorTracker.get(i).getColor());
//				}
				return true;
			}
			else if(x > 60 && x < 120 && y > 0 && y < 60)
			{
				selectedColor = Color.BLUE;
			
				if(PaintBrushView.colorTracker.size()!=0){
				Switcher temp=PaintBrushView.colorTracker.remove(PaintBrushView.colorTracker.size()-1); 
				temp.setEnd(PaintBrushView.xcord.size()-1);
				PaintBrushView.colorTracker.add(temp);}
				
				Switcher obj=new Switcher(PaintBrushView.xcord.size(), Color.BLUE, selectedThick);				
				PaintBrushView.colorTracker.add(obj);
				
//				System.out.println("*******************SELECTED BLUE************************");
//				for(int i=0;i<PaintBrushView.colorTracker.size();i++)
//				{
//					System.out.println("Start====>"+PaintBrushView.colorTracker.get(i).getStart());
//					System.out.println("End====>"+PaintBrushView.colorTracker.get(i).getEnd());
//					System.out.println("Color====>"+PaintBrushView.colorTracker.get(i).getColor());
//				}
				return true;
			}
			else if(x > 120 && x < 180 && y > 0 && y < 60)
			{
				selectedColor = Color.GREEN;
				
				
				if(PaintBrushView.colorTracker.size()!=0)
				{Switcher temp=PaintBrushView.colorTracker.remove(PaintBrushView.colorTracker.size()-1); 
				temp.setEnd(PaintBrushView.xcord.size()-1);
				PaintBrushView.colorTracker.add(temp);}
				
				Switcher obj=new Switcher(PaintBrushView.xcord.size(), Color.GREEN, selectedThick);				
				PaintBrushView.colorTracker.add(obj);
//				System.out.println("*******************SELECTED GREEN************************");
//				for(int i=0;i<PaintBrushView.colorTracker.size();i++)
//				{
//					System.out.println("Start====>"+PaintBrushView.colorTracker.get(i).getStart());
//					System.out.println("End====>"+PaintBrushView.colorTracker.get(i).getEnd());
//					System.out.println("Color====>"+PaintBrushView.colorTracker.get(i).getColor());
//				}
				
				return true;
			}
			else if(x > 180 && x < 240 && y > 0 && y < 60)
			{
				selectedColor = Color.MAGENTA;

				if(PaintBrushView.colorTracker.size()!=0){
				Switcher temp=PaintBrushView.colorTracker.remove(PaintBrushView.colorTracker.size()-1); 
				temp.setEnd(PaintBrushView.xcord.size()-1);
				PaintBrushView.colorTracker.add(temp);}
				
				Switcher obj=new Switcher(PaintBrushView.xcord.size(), Color.MAGENTA, selectedThick);				
				PaintBrushView.colorTracker.add(obj);
//				System.out.println("*******************SELECTED MAGENTA************************");
//				for(int i=0;i<PaintBrushView.colorTracker.size();i++)
//				{
//					System.out.println("Start====>"+PaintBrushView.colorTracker.get(i).getStart());
//					System.out.println("End====>"+PaintBrushView.colorTracker.get(i).getEnd());
//					System.out.println("Color====>"+PaintBrushView.colorTracker.get(i).getColor());
//				}
				return true;
			}
			else if(x > 240 && x < 300 && y > 0 && y < 60)
			{
				selectedColor = Color.YELLOW;

				if(PaintBrushView.colorTracker.size()!=0){
				Switcher temp=PaintBrushView.colorTracker.remove(PaintBrushView.colorTracker.size()-1); 
				temp.setEnd(PaintBrushView.xcord.size()-1);
				PaintBrushView.colorTracker.add(temp);}
				
				Switcher obj=new Switcher(PaintBrushView.xcord.size(), Color.YELLOW, selectedThick);				
				PaintBrushView.colorTracker.add(obj);
//				System.out.println("*******************SELECTED YELLOW************************");
//				for(int i=0;i<PaintBrushView.colorTracker.size();i++)
//				{
//					System.out.println("Start====>"+PaintBrushView.colorTracker.get(i).getStart());
//					System.out.println("End====>"+PaintBrushView.colorTracker.get(i).getEnd());
//					System.out.println("Color====>"+PaintBrushView.colorTracker.get(i).getColor());
//				}
				return true;
			}
			else if(x > 0 && x < 100 && y > 80 && y < 120)
			{
				p.setStrokeWidth(6);
				selectedThick = 1;
				Switcher s = PaintBrushView.colorTracker.get(PaintBrushView.colorTracker.size() - 1);
				s.setThickness(selectedThick);
				PaintBrushView.colorTracker.remove(PaintBrushView.colorTracker.size() -1);
				PaintBrushView.colorTracker.add(s);
				System.out.println("CLICK THICK");
				return true;
			}
			else if(x > 100 && x < 200 && y > 80 && y < 120)
			{
				//if selected thick is 0 then it means thin
				p.setStrokeWidth(2);
				selectedThick = 0;
				Switcher s = PaintBrushView.colorTracker.get(PaintBrushView.colorTracker.size() - 1);
				s.setThickness(selectedThick);
				PaintBrushView.colorTracker.remove(PaintBrushView.colorTracker.size() -1);
				PaintBrushView.colorTracker.add(s);
				System.out.println("CLICK THIN");
				//invalidate(); 
				return true;
			}
	  }
		
		
		
		
	return false;
	}
	

	
	
}


